package com.EAIPClub.UserBaseInfoDataManager.service.impl;

import com.EAIPClub.UserBaseInfoDataManager.mapper.UserBaseInfoDataManagerMapper;
import com.EAIPClub.UserBaseInfoDataManager.service.UserBaseInfoDataManagerService;
import com.api.ConstantPool;
import com.api.POJO.UserBaseInfoManager.BaseInfo;
import com.api.POJO.UserBaseInfoManager.BaseInfoReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :用户基本信息管理模块业务逻辑实现层
 * @Author :崔昌赫
 * @Date : 2021/10/21
 */
@Service
@Slf4j
public class UserBaseInfoDataManagerServiceImpl implements UserBaseInfoDataManagerService {

    private UserBaseInfoDataManagerMapper mapper;
    @Autowired
    public void setMapper(UserBaseInfoDataManagerMapper mapper) {
        this.mapper = mapper;
    }

    private BaseInfo inspectIsNull(BaseInfo baseInfo, BaseInfoReturnMsg oldBaseInfo){
        if(baseInfo.getUid()==null){
            log.info("检测到uid为空,将保留原uid.uid:{}",oldBaseInfo.getBaseInfo().getUid());
            baseInfo.setUid(oldBaseInfo.getBaseInfo().getUid());
        }
        if(baseInfo.getName()==null){
            log.info("检测到name为空,将保留原name.name:{}",oldBaseInfo.getBaseInfo().getName());
            baseInfo.setName(oldBaseInfo.getBaseInfo().getName());
        }
        if(baseInfo.getClasses()==null){
            log.info("检测到class为空,将保留原class.class:{}",oldBaseInfo.getBaseInfo().getClasses());
            baseInfo.setClasses(oldBaseInfo.getBaseInfo().getClasses());
        }
        return baseInfo;
    }

    @Override
    public BaseInfoReturnMsg findAllBaseInfo() {
        log.info("即将开始查询所有用户基本信息.");
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        List<BaseInfo> allBaseInfo = mapper.findAllBaseInfo();
        if(allBaseInfo==null){
            log.warn("查找所有用户基本信息失败!");
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("查找所有用户基本信息失败!");
        }
        log.info("查询成功!查询结果为:{}",allBaseInfo);
        returnMsg.setBaseInfoList(allBaseInfo);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg addNewBaseInfo(BaseInfo baseInfo) {
        log.info("即将开始添加用户基本信息.baseinfo:{}",baseInfo);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        if (!mapper.addNewBaseInfo(baseInfo)) {
            log.warn("添加用户基本信息失败!baseinfo:{}",baseInfo);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("添加用户基本信息失败!");
        }
        log.info("添加成功!信息为:{}",baseInfo);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg findBaseInfoByUid(String uid) {
        log.info("即将开始通过uid查找用户基本信息.uid:{}",uid);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfo baseInfoByUid = mapper.findBaseInfoByUid(uid);
        if (baseInfoByUid==null){
            log.warn("通过uid查找用户基本信息失败!uid:{}",uid);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid查找用户基本信息失败!");
        }
        log.info("查询成功!查询结果为:{}",baseInfoByUid);
        returnMsg.setBaseInfo(baseInfoByUid);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg findBaseInfoByName(String name) {
        log.info("即将开始通过name查找用户基本信息.name:{}",name);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        List<BaseInfo> baseInfoByName = mapper.findBaseInfoByName(name);
        if (baseInfoByName==null){
            log.warn("通过name查找用户基本信息失败!name:{}",name);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过name查找用户基本信息失败!");
        }
        log.info("查询成功!查询结果为:{}",baseInfoByName);
        returnMsg.setBaseInfoList(baseInfoByName);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg findBaseInfoByClass(String classes) {
        log.info("即将开始通过class查找用户基本信息.class:{}",classes);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        List<BaseInfo> baseInfoByClass = mapper.findBaseInfoByClass(classes);
        if (baseInfoByClass==null){
            log.warn("通过class查找用户基本信息失败!class:{}",classes);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过class查找用户基本信息失败!");
        }
        log.info("查询成功!查询结果为:{}",baseInfoByClass);
        returnMsg.setBaseInfoList(baseInfoByClass);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg findBaseInfoByStudentId(String studentId) {
        log.info("即将开始通过studentId查找用户基本信息.studentId:{}",studentId);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfo baseInfoByStudentId = mapper.findBaseInfoByStudentId(studentId);
        if (baseInfoByStudentId==null){
            log.warn("通过studentId查找用户基本信息失败!studentId:{}",studentId);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过studentId查找用户基本信息失败!");
        }
        log.info("查询成功!查询结果为:{}",baseInfoByStudentId);
        returnMsg.setBaseInfo(baseInfoByStudentId);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg updateBaseInfoByUid(String uid, BaseInfo baseInfo) {
        log.info("即将开始通过uid更新用户信息.uid:{},baseinfo:{}",uid,baseInfo);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByUid = findBaseInfoByUid(uid);
        if(!baseInfoByUid.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过uid查询到用户信息，本次更新失败!,uid:{},baseinfo:{},返回消息:{}",uid,baseInfo,baseInfoByUid.getMsg());
            Msg.add("未能通过uid查询到用户信息，本次更新失败!");
            Msg.addAll(baseInfoByUid.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        BaseInfo baseInfo1 = inspectIsNull(baseInfo, baseInfoByUid);
        if (!mapper.updateBaseInfoByUid(uid,baseInfo1)) {
            log.warn("通过uid更新用户信息失败!uid:{},baseinfo:{}",uid,baseInfo1);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid更新用户信息失败!");
        }
        log.info("更新完毕!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg updateBaseInfoByName(String name, BaseInfo baseInfo) {
        log.info("即将开始通过name更新用户信息.name:{},baseinfo:{}",name,baseInfo);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByName = findBaseInfoByName(name);
        if(!baseInfoByName.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过name查询到用户信息，本次更新失败!,name:{},baseinfo:{},返回消息:{}",name,baseInfo,baseInfoByName.getMsg());
            Msg.add("未能通过name查询到用户信息，本次更新失败!");
            Msg.addAll(baseInfoByName.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        BaseInfo baseInfo1 = inspectIsNull(baseInfo, baseInfoByName);
        if (!mapper.updateBaseInfoByName(name,baseInfo1)) {
            log.warn("通过name更新用户信息失败!name:{},baseinfo:{}",name,baseInfo1);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过name更新用户信息失败!");
        }
        log.info("更新完毕!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg updateBaseInfoByClass(String classes, BaseInfo baseInfo) {
        log.info("即将开始通过class更新用户信息.class:{},baseinfo:{}",classes,baseInfo);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByClass = findBaseInfoByClass(classes);
        if(!baseInfoByClass.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过class查询到用户信息，本次更新失败!,class:{},baseinfo:{},返回消息:{}",classes,baseInfo,baseInfoByClass.getMsg());
            Msg.add("未能通过class查询到用户信息，本次更新失败!");
            Msg.addAll(baseInfoByClass.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        BaseInfo baseInfo1 = inspectIsNull(baseInfo, baseInfoByClass);
        if (!mapper.updateBaseInfoByClass(classes,baseInfo1)) {
            log.warn("通过class更新用户信息失败!class:{},baseinfo:{}",classes,baseInfo1);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过class更新用户信息失败!");
        }
        log.info("更新完毕!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg updateBaseInfoByStudentId(String studentId, BaseInfo baseInfo) {
        log.info("即将开始通过studentid更新用户信息.studentid:{},baseinfo:{}",studentId,baseInfo);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByStudentId = findBaseInfoByStudentId(studentId);
        if(!baseInfoByStudentId.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过studentid查询到用户信息，本次更新失败!,studentid:{},baseinfo:{},返回消息:{}",studentId,baseInfo,baseInfoByStudentId.getMsg());
            Msg.add("未能通过studentid查询到用户信息，本次更新失败!");
            Msg.addAll(baseInfoByStudentId.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        BaseInfo baseInfo1 = inspectIsNull(baseInfo, baseInfoByStudentId);
        if (!mapper.updateBaseInfoByStudentId(studentId,baseInfo1)) {
            log.warn("通过studentid更新用户信息失败!studentid:{},baseinfo:{}",studentId,baseInfo1);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过studentid更新用户信息失败!");
        }
        log.info("更新完毕!");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg deleteBaseInfoByUid(String uid) {
        log.info("即将通过uid删除用户基本信息.uid:{}",uid);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByUid = findBaseInfoByUid(uid);
        if(!baseInfoByUid.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过uid查询到用户信息，本次删除失败!,uid:{},返回消息:{}",uid,baseInfoByUid.getMsg());
            Msg.add("未能通过uid查询到用户信息，本次删除失败!");
            Msg.addAll(baseInfoByUid.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (!mapper.deleteBaseInfoByUid(uid)) {
            log.warn("通过uid删除用户信息失败!uid:{}",uid);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid删除用户信息失败!");
        }
        log.info("删除完毕.");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg deleteBaseInfoByName(String name) {
        log.info("即将通过name删除用户基本信息.name:{}",name);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByName = findBaseInfoByName(name);
        if(!baseInfoByName.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过name查询到用户信息，本次删除失败!,name:{},返回消息:{}",name,baseInfoByName.getMsg());
            Msg.add("未能通过name查询到用户信息，本次删除失败!");
            Msg.addAll(baseInfoByName.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (!mapper.deleteBaseInfoByName(name)) {
            log.warn("通过name删除用户信息失败!name:{}",name);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过name删除用户信息失败!");
        }
        log.info("删除完毕.");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg deleteBaseInfoByClass(String classes) {
        log.info("即将通过class删除用户基本信息.class:{}",classes);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByClass = findBaseInfoByClass(classes);
        if(!baseInfoByClass.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过class查询到用户信息，本次删除失败!,class:{},返回消息:{}",classes,baseInfoByClass.getMsg());
            Msg.add("未能通过class查询到用户信息，本次删除失败!");
            Msg.addAll(baseInfoByClass.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (!mapper.deleteBaseInfoByClass(classes)) {
            log.warn("通过class删除用户信息失败!class:{}",classes);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过class删除用户信息失败!");
        }
        log.info("删除完毕.");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public BaseInfoReturnMsg deleteBaseInfoByStudentId(String studentId) {
        log.info("即将通过studentid删除用户基本信息.studentid:{}",studentId);
        BaseInfoReturnMsg returnMsg = new BaseInfoReturnMsg();
        List<String> Msg = new ArrayList<>();
        BaseInfoReturnMsg baseInfoByStudentId = findBaseInfoByStudentId(studentId);
        if(!baseInfoByStudentId.getStatus().equals(ConstantPool.OK)){
            log.warn("未能通过studentid查询到用户信息，本次删除失败!,studentid:{},返回消息:{}",studentId,baseInfoByStudentId.getMsg());
            Msg.add("未能通过studentid查询到用户信息，本次删除失败!");
            Msg.addAll(baseInfoByStudentId.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (!mapper.deleteBaseInfoByStudentId(studentId)) {
            log.warn("通过studentid删除用户信息失败!studentid:{}",studentId);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过studentid删除用户信息失败!");
        }
        log.info("删除完毕.");
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
